package com.hoshiicloud.user.controller.platform;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.common.rpc.Response;
import com.hoshiicloud.common.utils.BeanUtils;
import com.hoshiicloud.user.entity.platform.PlatformDepartments;
import com.hoshiicloud.user.param.platform.PlatformDepartmentsInsertParam;
import com.hoshiicloud.user.param.platform.PlatformDepartmentsParam;
import com.hoshiicloud.user.param.platform.PlatformDepartmentsUpdateParam;
import com.hoshiicloud.user.service.platform.PlatformDepartmentsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 部门 前端控制器
 * </p>
 *
 * @author yl
 * @since 2019-04-22
 */

@Slf4j
@Validated
@RestController
@RequestMapping("/platform/platformDepartments")
@Api(value = "租户平台部门", tags = {"租户平台部门"})
@ApiSort(22)
public class PlatformDepartmentsController {
    @Autowired
    private PlatformDepartmentsService platformDepartmentsService;

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 分页查询平台部门信息
     */
    @ApiOperation(value = "分页查询平台部门记录")
    @PostMapping("/getPage")
    public Response page(@RequestBody PlatformDepartmentsParam param) {
        try {
            QueryWrapper<PlatformDepartments> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().eq(StringUtils.isNotBlank(param.getName()),PlatformDepartments::getName, param.getName());
            queryWrapper.lambda().eq((null!=param.getPlatformId() && param.getPlatformId() != 0),PlatformDepartments::getPlatformId, param.getPlatformId());
            IPage<PlatformDepartments> page = new Page<>(Long.parseLong(param.getPageNumber()), Long.parseLong(param.getPageSize()));
            String key = param.toString()+ ",Page{pageNumber="+param.getPageNumber()+",pageSize="+param.getPageSize()+"}";
            return Response.data(platformDepartmentsService.getPage(page,queryWrapper,key));
        } catch (NumberFormatException e) {
            log.error("分页查询平台部门信息异常",e);
            return Response.failed("分页查询平台部门信息异常");
        }

    }

    /**
     * @param id
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据id查询平台部门信息
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询平台部门信息")
    public Response queryPlatformDepartmentsById(@PathVariable("id") Long id) {
        return Response.data(platformDepartmentsService.getById(id));
    }

    /**
     * @param name
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 根据名称查询
     */
    @GetMapping("/query/name/{name}")
    @ApiOperation(value = "查询平台部门信息")
    public Response queryPlatformDepartmentsByName(@PathVariable("name") String name) {
        return Response.data(platformDepartmentsService.queryPlatformDepartmentsByName(name));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 新增
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增平台部门信息")
    public Response savePlatformDepartments(@RequestBody @Valid PlatformDepartmentsInsertParam param) {
        PlatformDepartments entity ;
        try {
            entity = BeanUtils.covertType(param, PlatformDepartments.class);
        } catch (Exception e) {
            log.error("新增平台部门信息Bean转换异常",e);
            return Response.failed("新增平台部门信息Bean转换异常");
        }
        return Response.data(platformDepartmentsService.save(entity));
    }

    /**
     * @param param
     * @return com.hoshiicloud.common.rpc.Response
     * @description: 修改
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改平台部门信息")
    public Response updatePlatformDepartmentsById(@RequestBody @Valid PlatformDepartmentsUpdateParam param) {
        PlatformDepartments entity;
        try {
            entity = BeanUtils.covertType(param, PlatformDepartments.class);
        } catch (Exception e) {
            log.error("修改平台部门信息Bean转换异常",e);
            return Response.failed("修改平台部门信息Bean转换异常");
        }
        return Response.data(platformDepartmentsService.updateById(entity));
    }
}

